NHibernate.NHibernateException: Unable to locate row for retrieval of generated properties: [MyNames

Posted by Brad Heller on Stack Overflow See other posts from Stack Overflow or by Brad Heller
Published on 2010-06-13T06:38:44Z Indexed on 2010/06/13 6:42 UTC
Read the original article Hit count: 1134

Filed under:
|

It looks like all of my mappings are compiling correctly, I'm able to validly get a session from session factory. However, when I try to ISession.SaveOrUpdate(obj); I get this. Can anyone please help point me in the right direction?

private Configuration configuration;
protected Configuration Configuration {
    get
    {
        configuration = configuration ?? GetNewConfiguration();
        return configuration;
    }

}

protected ISession GetNewSession() {
    var sessionFactory = Configuration.BuildSessionFactory();
    var session = sessionFactory.OpenSession();
    return session; 

}

[TestMethod] public void
TestSessionSave() {
    var company = new Company();
    company.Name = "Test Company 1";

    DateTime savedAt = DateTime.Now;
    using (var session = GetNewSession())
    {
        try
        {
            session.SaveOrUpdate(company);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    Assert.IsTrue((company.CreationDate != null && company.CreationDate > savedAt), "Company was saved, creation date was set."); }

For those that might be interested, here is my mapping for this class:

  <!-- Company -->
  <class name="MyNamespace.Company,MyLibrary" table="Companies">
    <id name="Id" column="Id">
      <generator class="native" />
    </id>

    <property name="ExternalId" column="GUID" generated="insert" />
    <property name="Name" column="Name" type="string" />
    <property name="CreationDate" column="CreationDate" generated="insert" />
    <property name="UpdatedDate" column="UpdatedDate" generated="always" />
  </class>
  <!-- End Company -->

Finally, here is my config -- I'm just connecting to a SQL Server CE instance for this for testing purposes.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source="D:\Build\MyProject\Source\MyProject.UnitTests\MyProject.TestDatabase.sdf"</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate